package com.linkage.educloud.ah.mvp.presenter;

import android.content.Context;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.bean.AppBean;
import com.linkage.educloud.ah.bean.ArticleBean;
import com.linkage.educloud.ah.bean.BannerBean;
import com.linkage.educloud.ah.http.Method;
import com.linkage.educloud.ah.http.RequestUtil;
import com.linkage.educloud.ah.mvp.contract.IHomeContract;
import com.linkage.educloud.ah.mvp.model.HomeModel;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.subsciber.BaseSubscriber;
import com.zzhou.common.library.util.ToastShow;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observable;

/**
 * Created by Mr'Tang on 2018/9/5.
 */

public class HomePresenter extends IHomeContract.IPresenter<IHomeContract.IView> {
    Context mContext;
    IHomeContract.IModel iModel;

    public HomePresenter(Context context, IHomeContract.IView iView) {
        super(iView);
        mContext = context;
        iModel = new HomeModel();
    }

    @Override
    public void getRollAds(int usertype) {
        Observable observable = iModel.request(Method.POST, true, RequestUtil.getRollAds(String.valueOf(usertype)));
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("ret") == 0) {
                                ((IHomeContract.IView) iView).showBanner(BannerBean.parseFromJson(json.optJSONArray("data")));
                            } else {
                                ToastShow.show(mContext, json.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }

    @Override
    public void getMyApps(int type) {
        Observable observable = iModel.request(Method.POST, true, RequestUtil.getMyApps(type), Constants.Api.GET_APPS);
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("ret") == 0) {
                                ((IHomeContract.IView) iView).initApps(AppBean.parseFromJson(json.optJSONArray("data")));
                            } else {
                                ToastShow.show(mContext, json.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }

    @Override
    public void getArticle(int pageSize, int pages) {
        Observable observable = iModel.request(Method.POST, false, RequestUtil.getArticle(pageSize, pages),Constants.Api.GET_ARTICLES);
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("result") == 1) {
                                ((IHomeContract.IView) iView).initNews(ArticleBean.parseFromJson(json.optJSONArray("msglist")));
                            } else {
                                ToastShow.show(mContext, json.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }
}
